//
//  HLDetaiVideoRecommandView.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/11.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLDetaiVideoRecommandView.h"
#import <Masonry/Masonry.h>
#import "HLRecomandCell.h"
#import "HttpRequestManager.h"
#import "MBProgressHUD+HL.h"
#import "HLCourseRecommand.h"
#import "HLCourseDetail.h"
#import <MJExtension/MJExtension.h>

@interface HLDetaiVideoRecommandView ()<UITableViewDelegate,UITableViewDataSource>

/** tableView  */
@property (nonatomic ,strong) UITableView *tableView;

/** 标题 */
@property (nonatomic ,strong) UILabel *titleL;
/** 观看数 */
@property (nonatomic ,strong) UIButton *numB;
/** 分割线  */
@property (nonatomic ,strong) UIView *line;
/** 推荐 */
@property (nonatomic ,strong) UILabel *recommandL;
/** headView */
@property (nonatomic ,strong) UIView *headView;

/** NSMutableArray  */
@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation HLDetaiVideoRecommandView


- (NSMutableArray *)dataSource
{
    if (!_dataSource ) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UILabel *)titleL
{
    if (!_titleL ) {
        _titleL = [[UILabel alloc] init];
        _titleL.backgroundColor = [UIColor clearColor];
        _titleL.text = @"";
        _titleL.textColor = HLRGB(30, 30, 30);
        _titleL.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 13:( kMainTitleFontSize + 1 )];
    }
    return _titleL;
}

- (UIButton *)numB
{
    if (!_numB) {
        _numB = [[UIButton alloc]init];
        _numB.userInteractionEnabled = NO;
        _numB.titleLabel.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12: 14];
        _numB.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
        [_numB setImage:[UIImage imageNamed:@"home_course_play_num"] forState:UIControlStateNormal];
        [_numB setTitle:@"181.3万" forState:UIControlStateNormal];
        [_numB setTitleColor:HLRGB(120, 120, 120) forState:UIControlStateNormal];

    }
    return _numB;
}

- (UIView *)line
{
    if (!_line) {
        _line = [[UIView alloc]init];
        _line.backgroundColor = HLRGB(235, 235, 235);
    }
    return _line;
}

- (UILabel *)recommandL
{
    if (!_recommandL ) {
        _recommandL = [[UILabel alloc] init];
        _recommandL.textColor = HLRGB(30, 30, 30);
        _recommandL.text = @"为你推荐";
        _recommandL.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 13: 16];
    }
    return _recommandL;
}

- (UITableView *)tableView
{
    if (!_tableView ) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 100;
        _tableView.backgroundColor = HLRGB(245, 245, 245);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[HLRecomandCell class] forCellReuseIdentifier:@"HLRecomandCell"];
    }
    return _tableView;
}

- (UIView *)headView
{
    if (!_headView) {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = HLRGB(245, 245, 245);
        _headView.height = 120;
        [_headView addSubview:self.titleL];
        [_headView addSubview:self.numB];
        [_headView addSubview:self.line];
        [_headView addSubview:self.recommandL];
        
    }
    return _headView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        [self setUI];
        
        [self loadRequest];
    }
    return self;
}

- (void)loadRequest
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetRecommend signStr];
    params[@"course_id"] = self.courseid;
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetRecommend parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            weakSelf.dataSource = [HLCourseRecommand mj_objectArrayWithKeyValuesArray:responseObject[@"courses"]];
            [weakSelf.tableView reloadData];
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];

}

- (void)setUI
{
    self.backgroundColor = HLRGB(245, 245, 245);
    self.tableView.tableHeaderView = self.headView;
    [self addSubview:self.tableView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView).offset(10);
        make.top.equalTo(self.headView).offset(10);
        make.right.equalTo(self.headView).offset(-10);
        make.height.equalTo(@(30));
    }];
    
    [self.numB sizeToFit];
    [self.numB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView).offset(10);
        make.top.equalTo(self.titleL.mas_bottom);
        make.height.equalTo(@(30));
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.numB.mas_bottom).offset(10);
        make.left.equalTo(self.headView).offset(10);
        make.right.equalTo(self.headView).offset(-10);
        make.height.equalTo(@(1));
    }];
    
    [self.recommandL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView).offset(10);
        make.right.equalTo(self.headView).offset(10);
        make.top.equalTo(self.line.mas_bottom);
        make.height.equalTo(@(39));
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLRecomandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HLRecomandCell"];
    cell.courseRecommand = self.dataSource[indexPath.row];
    return cell;
}

- (void)setCourseDetail:(HLCourseDetail *)courseDetail
{
    _courseDetail = courseDetail;
    self.titleL.text = courseDetail.name;
    [self.numB setTitle:courseDetail.num forState:UIControlStateNormal];
    [self setNeedsDisplay];
}



@end
